package com.smartleanx.module.hrm.controller.admin.examine.examineRecord;

import cn.hutool.json.JSONObject;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import com.smartleanx.framework.excel.core.util.ExcelUtils;
import com.smartleanx.framework.operatelog.core.annotations.OperateLog;
import com.smartleanx.module.hrm.controller.admin.examine.examineRecord.vo.ExamineRecordPageReqVO;
import com.smartleanx.module.hrm.controller.admin.examine.examineRecord.vo.ExamineRecordRespVO;
import com.smartleanx.module.hrm.controller.admin.examine.examineRecord.vo.ExamineRecordSaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineRecord.ExamineRecordDO;
import com.smartleanx.module.hrm.service.examine.examineRecord.ExamineRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.smartleanx.framework.common.pojo.CommonResult.success;
import static com.smartleanx.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 考核计划开始后生成记录")
@RestController
@RequestMapping("/hrm/examine-record")
@Validated
public class ExamineRecordController {

    @Resource
    private ExamineRecordService examineRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建考核计划开始后生成记录")
    @PreAuthorize("@ss.hasPermission('hrm:examine-record:create')")
    public CommonResult<Integer> createExamineRecord(@Valid @RequestBody ExamineRecordSaveReqVO createReqVO) {
        return success(examineRecordService.createExamineRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考核计划开始后生成记录")
    @PreAuthorize("@ss.hasPermission('hrm:examine-record:update')")
    public CommonResult<Boolean> updateExamineRecord(@Valid @RequestBody ExamineRecordSaveReqVO updateReqVO) {
        examineRecordService.updateExamineRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考核计划开始后生成记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:examine-record:delete')")
    public CommonResult<Boolean> deleteExamineRecord(@RequestParam("id") Integer id) {
        examineRecordService.deleteExamineRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考核计划开始后生成记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:examine-record:query')")
    public CommonResult<ExamineRecordRespVO> getExamineRecord(@RequestParam("id") Integer id) {
        ExamineRecordDO examineRecord = examineRecordService.getExamineRecord(id);
        return success(BeanUtils.toBean(examineRecord, ExamineRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考核计划开始后生成记录分页")
    @PreAuthorize("@ss.hasPermission('hrm:examine-record:query')")
    public CommonResult<PageResult<ExamineRecordRespVO>> getExamineRecordPage(@Valid ExamineRecordPageReqVO pageReqVO) {
        PageResult<ExamineRecordDO> pageResult = examineRecordService.getExamineRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExamineRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考核计划开始后生成记录 Excel")
    @PreAuthorize("@ss.hasPermission('hrm:examine-record:export')")
    @OperateLog(type = EXPORT)
    public void exportExamineRecordExcel(@Valid ExamineRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExamineRecordDO> list = examineRecordService.getExamineRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考核计划开始后生成记录.xlsx", "数据", ExamineRecordRespVO.class,
                        BeanUtils.toBean(list, ExamineRecordRespVO.class));
    }


}